package com.wx.rabbitmq.dao;

import com.wx.rabbitmq.entity.BrokerMessageLog;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * @author wx
 * @Description: 消息dao
 * @date 2018/12/3 000316:02
 */
@Mapper
public interface BrokerMessageLogDao {
    /**
     * @param record
     * @description: 消息入库
     * @author: wx
     * @date: 2018/12/3 0003
     * @return: int
     */
    @Insert("insert into broker_message_log(message_id, message,try_count,status,next_retry,create_time,update_time)" +
            "values(#{messageId}, #{message}, #{tryCount},#{status},#{nextRetry},#{createTime},#{updateTime})")
    int insert(BrokerMessageLog record);

    /**
     * @description: 查询消息状态为0(发送中) 且已经超时的消息集合
     * @author: wx
     * @date: 2018/12/3 0003
     * @param
     * @return: java.util.List<com.wx.rabbitmq.entity.BrokerMessageLog>
     */
    @Select("select * from broker_message_log where status in('0','1') and  next_retry <= now() ")
    List<BrokerMessageLog> queryStatusAndTimeoutMessage();

    /**
     * @description: 重新发送统计count发送次数 +1
     * @author: wx
     * @date: 2018/12/3 0003
     * @param messageId 消息id
     * @param updateTime 操作时间
     * @return: void
     */
    @Update("update broker_message_log set try_count = try_count + 1, update_time = #{updateTime} where message_id = #{messageId}")
    void updateReSend(@Param("messageId")String messageId, @Param("updateTime")Date updateTime);
    /**
     * @description: 更新最终消息发送结果
     * @author: wx
     * @date: 2018/12/3 0003
     * @param messageId 消息id
     * @param status 消息状态
     * @param updateTime 操作时间
     * @return: void
     */
    @Update("update broker_message_log set status = #{status}, update_time = #{updateTime} where message_id = #{messageId}")
    void changeBrokerMessageLogStatus(@Param("messageId")String messageId, @Param("status")String status, @Param("updateTime")Date updateTime);
}
